package whatIsThis;

import java.util.Scanner;
class ceshi {
	public static void main(String[] args) {
		int choice;
		Scanner scanner=new Scanner(System.in);
		while (true){
			System.out.println("1、时间片轮转调度 2、优先级调度 0、退出");
			choice=scanner.nextInt();
			switch (choice){
				case 1:
					timeslice();
					break;
				case 2:
					priority();
					break;
				case 0:
					System.out.println("欢迎下次使用");
					return;
				default:
					System.out.println("请重新输入！");
			}
		}
	}
	static class PCB {
		String name;
		String state;
		int cpuTime;
		int count;
		int needTime;
		int round;
		int priority;
	}
	static void timeslice() {
		int i;
		int n;
		int max;
		int j;
		int y;
		System.out.println("输入时间片");
		Scanner scanner = new Scanner(System.in);
		y=scanner.nextInt();
		System.out.println("输入进程数");
		n = scanner.nextInt();
		String[] ready = new String[n];
		String[] finish = new String[n];
		String[] wait = new String[n];
		scanner.nextLine();
		PCB[] pcb = new PCB[2*n];
		System.out.println("输入名字和需要时间:");
		for (i = 0; i < n; i++) {
			String num;
			num = scanner.nextLine();
			String[] split = num.split(" ");
			String name = split[0];
			String needtime = split[1];
			PCB a = new PCB();
			a.name = name;
			a.needTime = Integer.parseInt(needtime);
			pcb[i] = a;
		}
		max = pcb[0].needTime;
		for (i = 0; i < n; i++) {
			if (pcb[i].needTime > max)
				max = pcb[i].needTime;
		}
		int a = (int) Math.ceil(max / 2.0);
		for (i = 0; i < n; i++) {
			pcb[i].round = a;
			pcb[i].state = "W";
			pcb[i].cpuTime = 0;
			pcb[i].count = 0;
		}
		int[]w=new int [n];
		for (i=0;i<n;i++){
			w[i]=pcb[i].needTime;
		}
		while (true) {
			int k = 0, u = 0,z=0;
			PCB t;
			t=new PCB();
			for (i=0;i<n;i++){
				if (!pcb[i].state.equals("F")) {
					pcb[i].state = "R";
					break;
				}
			}
			for (i = 0; i < n; i++) {
				if (pcb[i].state.equals("R")){
					ready[z]=pcb[i].name;
					pcb[n]=pcb[i];
					z++;
				}
			}
			for (i=0;i<n;i++){
				if (pcb[i].state.equals("W")){
					wait[k]=pcb[i].name;
					pcb[n+z+k]=pcb[i];
					k++;
				}
			}
			for (i=0;i<n;i++){
				if (pcb[i].state.equals("F")){
					finish[u]=pcb[i].name;
					pcb[n+z+k+u]=pcb[i];
					u++;
				}
			}
			System.out.print("Name      cputime   needtime  count     round     state\n");
			for (i = 0; i < n; i++) {
				System.out.printf("%-10s%-10d%-10d%-10d%-10d%-10s\n", pcb[n+i].name, pcb[n+i].cpuTime, pcb[n+i].needTime, pcb[n+i].count, pcb[n+i].round, pcb[n+i].state);
			}
			System.out.print("就绪队列:");
			for (i = 0; i < k; i++) {
				System.out.print(ready[i]+" ");
			}
			System.out.print("\n");
			System.out.print("完成队列:");
			for (i=0;i<u;i++){
				System.out.print(finish[i]+" ");
			}
			System.out.print("\n");
			if (finish[n - 1] != null) {
				break;
			}
			PCB b = new PCB();
			int d=0;
			for (j = 0; j < n; j++) {
				if (pcb[j].needTime != 0) {
					b = pcb[j];
					d = w[j];
					break;
				}
			}
			for (i = j + 1; i < n; i++) {
				pcb[i - 1] = pcb[i];
				w[i - 1] = w[i];
			}
			pcb[n - 1] = b;
			w[n - 1] = d;
			for (int r = y; r > 0; r--) {
				if ((pcb[n - 1].cpuTime + r) <= w[n - 1]) {
					pcb[n - 1].cpuTime = pcb[n - 1].cpuTime + r;
					break;
				}
			}
			pcb[n - 1].needTime = pcb[n - 1].needTime - y;
			pcb[n - 1].count++;
			if (pcb[n - 1].needTime <= 0) {
				pcb[n - 1].needTime = 0;
				pcb[n - 1].state = "F";
			} else {
				pcb[n - 1].state = "W";
			}
		}
	}
	static void priority(){
		int i,n,j,y,count=0;
		System.out.println("输入时间片");
		Scanner scanner = new Scanner(System.in);
		y=scanner.nextInt();
		System.out.println("输入进程数");
		n = scanner.nextInt();
		String[] ready = new String[n];
		String[] finish = new String[n];
		String[] wait = new String[n];
		scanner.nextLine();
		PCB[] pcb = new ceshi.PCB[n];
		System.out.println("输入名字和需要时间:");
		for (i = 0; i < n; i++) {
			String num;
			num = scanner.nextLine();
			String[] split = num.split(" ");
			String name = split[0];
			String needtime = split[1];
			PCB a = new ceshi.PCB();
			a.name = name;
			a.needTime = Integer.parseInt(needtime);
			a.priority=50-a.needTime;
			pcb[i] = a;
		}
		for (i = 0; i < n; i++) {
			pcb[i].state = "W";
			pcb[i].cpuTime = 0;
			pcb[i].count = 0;
		}
		for (i=0;i<n-count;i++){
			for (int e=i;e<n-count;e++){
				if (pcb[i].priority<pcb[e].priority){
					PCB v=new PCB();
					v=pcb[e];
					pcb[e]=pcb[i];
					pcb[i]=v;
				}
			}
		}
		int[]w=new int [n];
		for (i=0;i<n;i++){
			w[i]=pcb[i].needTime;
		}
		while (true) {
			int k = 0, u = 0,z=0;
			for (i=0;i<n;i++){
				if (!pcb[i].state.equals("F")) {
					pcb[i].state = "R";
					break;
				}}
			for (i = 0; i < n; i++) {
				if (pcb[i].state.equals("R")){
					ready[z]=pcb[i].name;
					z++;
				}
			}
			for (i=0;i<n;i++){
				if (pcb[i].state.equals("W")){
					wait[k]=pcb[i].name;
					k++;
				}
			}
			for (i=0;i<n;i++){
				if (pcb[i].state.equals("F")){
					finish[u]=pcb[i].name;
					u++;
				}
			}
			System.out.print("Name      cputime   needtime  count     pri       state\n");
			if (finish[n-1]!=null) {for (i = 0; i < n; i++) {
				System.out.printf("%-10s%-10d%-10d%-10d%-10d%-10s\n", pcb[i].name, pcb[i].cpuTime, pcb[i].needTime, pcb[i].count, pcb[i].priority, pcb[i].state);
			}
			}
			else {
				for (i=0;i<n-count;i++){
					System.out.printf("%-10s%-10d%-10d%-10d%-10d%-10s\n", pcb[i].name, pcb[i].cpuTime, pcb[i].needTime, pcb[i].count, pcb[i].priority, pcb[i].state);
				}
				for (i=n-1;i>n-1-count;i--){
					System.out.printf("%-10s%-10d%-10d%-10d%-10d%-10s\n", pcb[i].name, pcb[i].cpuTime, pcb[i].needTime, pcb[i].count, pcb[i].priority, pcb[i].state);
				}
			}
			System.out.print("就绪队列:");
			for (i = 0; i < k; i++) {
				System.out.print(wait[i]+" ");
			}
			System.out.print("\n");
			System.out.print("完成队列:");
			for (i=0;i<u;i++){
				System.out.print(finish[i]+" ");
			}
			System.out.print("\n");
			if (finish[n - 1] != null) {
				break;
			}
			PCB b = new ceshi.PCB();
			int d=0;
			for (i=0;i<n-count;i++){
				for (int e=i;e<n-count;e++){
					if (pcb[i].priority<pcb[e].priority){
						PCB v=new PCB();
						v=pcb[e];
						pcb[e]=pcb[i];
						pcb[i]=v;
					}
				}
			}
			for (j = 0; j < n; j++) {
				if (pcb[j].needTime != 0) {
					b = pcb[j];
					d = w[j];
					break;
				}
			}
			for (i = j + 1; i < n-count; i++) {
				pcb[i - 1] = pcb[i];
				w[i - 1] = w[i];
			}
			pcb[n - 1-count] = b;
			w[n - 1-count] = d;
			for (int r = y; r > 0; r--) {
				if ((pcb[n - 1-count].cpuTime + r) <= w[n - 1-count]) {
					pcb[n - 1-count].cpuTime = pcb[n - 1-count].cpuTime + r;
					break;
				}
			}
			pcb[n - 1-count].needTime = pcb[n - 1-count].needTime - y;
			pcb[n - 1-count].count++;
			if (pcb[n - 1-count].needTime <= 0) {
				pcb[n - 1-count].needTime = 0;
				pcb[n - 1-count].state = "F";
				b=pcb[n-1-count];
				for (i=0;i<count;i++){
					pcb[n-count+i-1]=pcb[n-count+i];
				}
				pcb[n-1]=b;
				count++;
			} else {
				pcb[n - 1-count].state = "W";
				pcb[n-1-count].priority=pcb[n-1-count].priority-3;
			}
		}
	}
}
